package org.porridge.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.porridge.po.EntityPo;
import org.porridge.po.RecordSubmit;
import org.porridge.po.Resource;
import org.porridge.po.SeachLog;
import org.porridge.util.FileUtil;
import org.porridge.util.SVNManager;

/**
 * 
 * <p>Title: createfile   </p>
 * <p>Description: 打包数据</p>
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: svnkit</p>
 * <p>Create Date/Time: 2014年8月12日 下午8:21:13<p>
 * @author porridge
 * @version 1.0
 * @url www.prdblog.com
 *
 */
public class createfile extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SVNManager svn=(SVNManager) request.getSession().getAttribute("svn");
		if(svn!=null){
				SeachLog seach=new SeachLog();// 填充搜索条件
				Field[] fies=seach.getClass().getDeclaredFields();
				Object obj=null;
				for(Field f:fies){
					f.setAccessible(true);
					obj=request.getParameter(f.getName());
					if(obj!=null && obj.toString().trim().length()>0)
						try {
							f.set(seach,obj.toString().trim());
						} catch (Exception e) {
							e.printStackTrace();
						}
				}
				if(seach.getUsername()!=null && seach.getUsername().trim().length()>0)
					seach.setUsername(";"+seach.getUsername()+";");
				if(seach.getLocalpath()!=null && seach.getCreateproject()!=null){
					response.setCharacterEncoding("UTF-8");
					PrintWriter out=response.getWriter();
					try {
						List<Resource> resu=svn.filterCommitLog(seach);//先把需要打包的数据查询出来
						
						String url=seach.getCreateproject();
						
						SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日mm时hh分ss秒");
						url+=File.separatorChar+sdf.format(new Date())+"_自动生成版本"+File.separator;
						
						StringBuffer delstb=new StringBuffer();
						String delpath="";
						if(seach.getDelpath()!=null && seach.getDelpath().trim().length()>0)
							delpath=seach.getDelpath();
						
						List<String> updatelog=new ArrayList<String>();//更新记录
						List<String> sqllog=new ArrayList<String>();//sql记录
						String tempsql="";//临时装sql使用
						boolean bl=false;//判断是否正在读取sql更新语句
						
						for(Resource re:resu){
							for(RecordSubmit r:re.getRes()){
								if(!"D".equals(r.getType())){//只要不是删除的就OK了.都记录
									String tpath=r.getPath();
									if("true".equals(seach.getIsjowwei())){//如果是公司生成项目的情况
										String rep="/modulbase/";
										if(tpath.indexOf(rep)!=-1){
											String t1=tpath.substring(0,tpath.indexOf(rep));
											String t2=tpath.substring(tpath.indexOf(rep)+rep.length(),tpath.length());
											tpath=t1+"/webapp/WEB-INF/classes/"+t2;
											tpath=tpath.replace(".java",".class");
										}
										else if(tpath.indexOf("resource")!=-1)
											tpath=tpath.replace("resource","webapp/WEB-INF/classes");
										//以上代码是针对公司做的特殊处理
									}
									FileUtil.copydoc(seach.getLocalpath()+tpath.replace(delpath,""),url+tpath.replace(delpath,""));
								}else{//写日志删除的
									delstb.append(r.getPath().replace("delpath","")+"\n");
								}
								//A 指添加数据   D 指删除数据   M指修改数据
							}
							if(re.getRemark()!=null && re.getRemark().length()>0){
								String[] remarks=re.getRemark().split("\n");
								tempsql="";
								bl=false;
								for(String remark:remarks){
									remark=remark.trim();//
									if(remark.length()>0){
										if(!bl && remark.indexOf("log:")==0)//
											updatelog.add(remark.replace("log:","")+" --更新人"+re.getSubmituser());
										if(bl && remark.indexOf("sql_end")==0){
											tempsql+=remark.replace("sql_end","");
											sqllog.add(tempsql+"\n --更新人"+re.getSubmituser());
											tempsql="";
											bl=false;
										}
										if(bl || remark.indexOf("sql_start")==0){
											bl=true;
											tempsql+=remark.replace("sql_start","")+"\n";
										}
									}
								}
							}
						}
						StringBuffer stb=new StringBuffer();
						stb.append(sdf.format(new Date())+"更新日志\n本次更新功能数量:"+updatelog.size()+"\n本次更新sql语句数量:"+sqllog.size()+"\n");
						stb.append("\n===========================================\n\n");
						for(int i=0;i<updatelog.size();i++){
							stb.append((i+1)+"."+updatelog.get(i)+"\n");
						}
						stb.append("\n===========================================\n\n");
						for(int i=0;i<sqllog.size();i++){
							stb.append("--第"+(i+1)+"次提交sql:\n\n"+sqllog.get(i)+"\n\n");
						}
						if(delstb.length()>0){
							stb.append("\n===========================================\n\n");
							stb.append("本次更新删除数据如下:\n"+delstb.toString());
						}
						FileUtil.setfile(url+"更新日志.txt",stb.toString());
						
						out.print("true");
					} catch (Exception e) {
						out.print("false");
						e.printStackTrace();
					}
				}
		}
	}
}
